using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CodeStory.Core.UI;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core;
using Activa.CoolStorage;
using System.IO;
using CodeStory.Core.Services;
using System.Collections.Generic;

namespace CodeStory.WebApplication.Core.Admin
{
	public partial class ContainerManager : Portlet
	{

		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(ContainerManager_Load);
			btnInstall.Click += new EventHandler(ButtonInstall_Click);
			base.OnInit(e);
		}
		void ButtonInstall_Click(object sender, EventArgs e) {
			foreach (GridViewRow row in notinstalledGridView.Rows) {
				CheckBox chk = (CheckBox)row.FindControl("chkAdd");
				if (!chk.Checked) continue;
				TextBox txtFriendlyName = (TextBox)row.FindControl("txtFriendlyName");
				TextBox txtDesc = (TextBox)row.FindControl("txtCodeName");
				Label lblPath = (Label)row.FindControl("lblPath");
				PortletTemplateObject obj = PortletTemplateObject.New();
				obj.FriendlyName = txtFriendlyName.Text;
				obj.CodeName = txtDesc.Text;
				obj.TemplateFilePath = lblPath.Text;
				obj.TemplateID = Guid.NewGuid();
				obj.Save();
			}
			BindInstalledPortlets();
		}

		void ContainerManager_Load(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			if (EditMode) return;
			if (!IsPostBack) {
				BindInstalledPortlets();
			}
		}

		private void BindInstalledPortlets() {
			CSList<PortletTemplateObject> list = PortletTemplateObject.List();
			installedGridView.DataSource = list;
			installedGridView.DataBind();

			BindNotInstalledPortlets(list);
		}

		private void BindNotInstalledPortlets(CSList<PortletTemplateObject> list) {
			string skin = FileUtilityService.Combine(Request.PhysicalApplicationPath,"Skins");
			DirectoryInfo di = new DirectoryInfo(skin);

			List<FileItem> l = new List<FileItem>();
			foreach (DirectoryInfo d in di.GetDirectories()) {
				string con = FileUtilityService.Combine(d.FullName,"Container");
				DirectoryInfo cd = new DirectoryInfo(con);
				if (!cd.Exists) continue;
				foreach (FileInfo fi in cd.GetFiles("*.ascx")) {
					FileItem item = new FileItem(fi.FullName, con);
					bool found = FindTemplate(list,item.ContainerPath);
					if (found) continue;
					l.Add(item);
				}
			}
			notinstalledGridView.DataSource = l;
			notinstalledGridView.DataBind();
		}

		private bool FindTemplate(CSList<PortletTemplateObject> list, string p) {
			foreach (PortletTemplateObject obj in list) {
				if (obj.TemplateFilePath.Equals(p, StringComparison.OrdinalIgnoreCase)) return true;
			}
			return false;
		}

		private class FileItem
		{
			private string fullName = string.Empty;
			private string containerPath = string.Empty;
			string prePath = string.Empty;
			public FileItem(string fullName, string prePath) {
				this.FullName = fullName;
				this.prePath = prePath;
			}

			public string FullName {
				get { return fullName; }
				set {
					fullName = value.Replace("\\", "/");
				}
			}
			public string ContainerPath {
				get {
					if (string.IsNullOrEmpty(containerPath)) {
						containerPath = FullName.Substring(prePath.Length).Trim('/');
					}
					return containerPath;
				}
			}
		}

	}
}